package com.ruoyi.lightkillPest.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.lightkillPest.domain.device.SCDDeviceDao;
import com.ruoyi.lightkillPest.domain.device.SelectSCDDeviceDao;
import com.ruoyi.lightkillPest.mapper.SCDDeviceMapper;
import com.ruoyi.lightkillPest.service.SCDDeviceService;
import com.ruoyi.tcp.domain.ChannelMap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import static com.ruoyi.tcp.tcpserver.TCPServerHandler.CRC16_HT212;

@Slf4j
@RestController
@RequestMapping("/SCD")
public class SCDTCPServerController extends ChannelInboundHandlerAdapter {



    @Autowired
    private SCDDeviceMapper scdDeviceMapper;

    @Autowired
    private SCDDeviceService scdDeviceService;

//    @PostMapping("/setMode")
//    public AjaxResult setMode(String workMode) {
//        globalVariable.setWorkMode(workMode);
//        return AjaxResult.success("设置成功");
//    }


    /**
     * 远程设置杀虫灯设备的工作模式和工作时长
     * @param selectSCDDeviceDao
     * @return
     */
    @PostMapping("/control")
    public AjaxResult control(SelectSCDDeviceDao selectSCDDeviceDao){
        String deviceId = selectSCDDeviceDao.getDeviceId();
        String modeHours = selectSCDDeviceDao.getModeHours();
        Integer workMode = selectSCDDeviceDao.getWorkMode();
        String beginTime = selectSCDDeviceDao.getBeginTime();

        SCDDeviceDao scdDeviceDao = new SCDDeviceDao().setIsRollback(1);
        scdDeviceDao.setDeviceId(deviceId).setModeHours(modeHours).setWorkMode(workMode).setBeginTime(beginTime);
//        int i = scdDeviceMapper.updateSCDDeviceInfo(scdDeviceDao);
        Integer i = scdDeviceService.updateSCDDeviceInfo(scdDeviceDao);
        if(i == null){
            return AjaxResult.error("参数设置错误");
        }else if(i > 0){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败");
        }
    }

    /**
     * 远程设置多台杀虫灯设备的工作模式和工作时长
     * @param
     * @return
     */
    @PostMapping("/controls")
    public AjaxResult controls(SelectSCDDeviceDao selectSCDDeviceDao){
        int successCount = 0;
        String modeHours = selectSCDDeviceDao.getModeHours();
        Integer workMode = selectSCDDeviceDao.getWorkMode();
        String beginTime = selectSCDDeviceDao.getBeginTime();
        String deviceIds = selectSCDDeviceDao.getDeviceIds();
        String[] items = deviceIds.split(",");
        List<String> deviceIdList = new ArrayList<>(Arrays.asList(items));
        for(String deviceId :deviceIdList) {
            SCDDeviceDao scdDeviceDao = new SCDDeviceDao().setIsRollback(1);
            scdDeviceDao.setDeviceId(deviceId).setModeHours(modeHours).setWorkMode(workMode).setBeginTime(beginTime);
//            int i = scdDeviceMapper.updateSCDDeviceInfo(scdDeviceDao);
            Integer i = scdDeviceService.updateSCDDeviceInfo(scdDeviceDao);
            if (i == null) {
                return AjaxResult.error("参数设置错误");
            } else if (i > 0) {
                successCount++;
            }
        }
        if(successCount == deviceIdList.size()){
            return AjaxResult.success("设置成功");
        }else{
            return AjaxResult.error("设置失败");
        }
    }




}
